package keqiang.com.kq_flutter_core_widget.channel;

import android.app.Activity;
import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import keqiang.com.kq_flutter_core_widget.interf.IKqRoute;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;

/**
 * flutter 关闭原生页面通道
 *
 * @author zhouzhuo810
 * @date 2023/3/13 17:49
 */
public class NativeNavigateChannel extends MethodChannel {
    
    // 通道id
    private static final String CHANNEL = "flutter.keqiang.com/navigation";
    
    private static final String METHOD_CLOSE = "close";
    private static final String METHOD_OPEN = "open";
    private static final String METHOD_EXIT_LOGIN = "exitLogin";
    
    private static final String METHOD_SET_ORIENTATION_MODE = "setOrientationMode";
    
    private Context mContext;
    
    public NativeNavigateChannel(@NonNull BinaryMessenger messenger) {
        super(messenger, CHANNEL);
        
        init();
    }
    
    
    public void setContext(Context context) {
        mContext = context;
    }
    
    private void init() {
        setMethodCallHandler((call, result) -> {
            switch (call.method) {
                case METHOD_CLOSE:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqRoute) {
                            ((IKqRoute) mContext).closeAct();
                        } else if (mContext instanceof BaseActivity) {
                            ((BaseActivity) mContext).closeAct();
                        } else if (mContext instanceof Activity) {
                            ((Activity) mContext).finish();
                        }
                    }
                    break;
                case METHOD_OPEN:
                    if (mContext != null) {
                        if (mContext instanceof IKqRoute) {
                            String pageName = call.argument("pageName");
                            Map<String, Object> params = call.argument("params");
                            ((IKqRoute) mContext).openAct(result, pageName, params);
                        } else {
                            result.error("0", "no method Implementation", null);
                        }
                    } else {
                        result.error("0", "context is null", null);
                    }
                    break;
                case METHOD_EXIT_LOGIN:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqRoute) {
                            ((IKqRoute) mContext).exitLogin();
                        }
                    }
                    break;
                case METHOD_SET_ORIENTATION_MODE:
                    result.success(true);
                    if (mContext != null) {
                        if (mContext instanceof IKqRoute) {
                            Integer pageName = call.argument("orientationMode");
                            if (pageName != null) {
                                ((IKqRoute) mContext).setOrientationMode(pageName);
                            }
                        }
                    }
                    break;
            }
        });
    }
    
}
